import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UnitsProvider } from '../../providers/units/units';
/**
 * Generated class for the AssistantqingjiaPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-assistantqingjia',
  templateUrl: 'assistantqingjia.html',
})
export class AssistantqingjiaPage {

  /**
	 * 输入界面控制
	 */
  inputSelectView = {
    applyoaleaveDefItems: new Array(), //请假种类
    applyoaminDate: '',
    applyoamaxDate: '',
  }

  /**
	 * 输入界面控制
	 */
  inputViewModel = {
    applyapplymember: '', //姓名
    applyapplytypes: new Object(), //请假种类
    applytime: '', //申请日期
    applycompany: new Object(), //申请公司
    applydepartment: new Object(), //申请部门
    applychangenote: '', //请假事由
    applyholidaystarttime: '', //开始时间
    applyholidayendtime: '', //结束时间
    applyholidaydays: '', //请假天数
    applyholidayaddress: '', //家庭住址
    applyholidaytel: '', //联系电话
    applyholidayjob: '', //职务
    applyholidayjointime: '', //参加工作时间
    applyholidayworkage: '', //工龄(年)
    applyholidayyeardays: '', //年休假标准(天)
    applyholidaymember: '', //请假人
    applynote: '', //备注
    applynownode: '', //当前节点
    applynowmember: '', //当前处理人
    applynextnode: '', //下一步审批节点
    applynextman: '', //下一步审批人
  }

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public http: HttpProvider,
    public storage: StorageProvider,
    public dialog: DialogProvider,
    public units: UnitsProvider) {
    this.inputSelectView.applyoaminDate = '2010-01-01';
    this.inputSelectView.applyoamaxDate = this.units.getinitDate();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad AssistantqingjiaPage');
    this.ionicLoad();
  }

  //当前节点
  ionicLoad() {
    //控制显示
    this.getUserInfo();
    this.loadaoleaveDefData();
    this.inputViewModel.applytime = this.units.getCurrentDate(new Date().getTime());
    this.inputViewModel.applynownode = '提交申请'; //当前节点
    this.inputViewModel.applynextnode = '部门内控办理'; //当前节点
  }
  //获取本用户信息
  getUserInfo() {
    this.storage.getStorage("user").then(val => {
      //请求方式如下
      let params = {
        user_id: val['user_id']
      };
      this.http.postJson('user', 'douserinfo.do', params).then(res => {
        if (res['success'] == true) {
          let info = res['data'];
          //请求成功
          this.inputViewModel.applyapplymember = info['name'];
          this.inputViewModel.applynowmember = info['name'];
          this.inputViewModel.applyholidaymember = info['name'];
        } else {
          this.dialog.showToast("top", res['msg']);
        }
      });
    });
  }
  //初始化变更类型
  loadaoleaveDefData() {
    //初始化
    let params = new Object();
    params['type'] = 'oa_leave2_type';
    this.http.postJson('dict', 'getDictByType.do', params).then(res => {
      let datas = res['data']['dictList'];
      this.inputSelectView.applyoaleaveDefItems = new Array();
      for (let i = 0; i < datas.length; i++) {
        let item = datas[i];
        this.inputSelectView.applyoaleaveDefItems.push({
          id: item['value'],
          name: item['label']
        });
      }
    });
  }
  //初始化选择公司
  applycompanyselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applycompany;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModalcompanyselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applycompany = result[0];
      }
    });
  }
  //初始化选择部门
  applydepartselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applydepartment;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModaldepartselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applydepartment = result[0];
      }
    });
  }
  //初始化选择用户
  applyapplymemberselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applynextman;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModaluserselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applynextman = result[0];
      }
    });
  }
  //时间选择
  applychangeDate(event: any) {
    this.getJebYear(this.inputViewModel.applyholidaystarttime);
  }
  //初始化入职年限(请假开始时间计算入职时间和标准)
  getJebYear(leaveStartDays: any) {
    this.storage.getStorage("user").then(val => {
      //请求方式如下
      let params = {
        user_id: val['user_id'],
        leaveStartDays: leaveStartDays
      };
      this.http.postJson('oa/leave', 'getJebYear.do', params).then(res => {
        if (res['success'] == true) {
          let info = res['data'];
          //workYear 工龄; leaveDays 年休假标准; entry 入职时间
          this.inputViewModel.applyholidayjointime = this.units.dateToISOString(info['entry']);//入职时间
          this.inputViewModel.applyholidayyeardays = info['leaveDays'];//年休假标准
          this.inputViewModel.applyholidayworkage = info['workYear'];//工龄
        } else {
          this.dialog.showToast("top", res['msg']);
        }
      });
    });
  }
  //变更类型对比选中
  compareselect(e1: Object, e2: Object) {
    return e1 && e2 ? e1['id'] === e2['id'] : e1 === e2;
  }

  //提交数据
  applyapplydone() {
    if (this.checkInput()) {
      //初始化
      this.storage.getStorage("user").then(val => {
        //请求方式如下
        let params = {
          user_id: val['user_id'],
          leaveType: this.inputViewModel.applyapplytypes['id'],
          company: this.inputViewModel.applycompany['id'],
          dept: this.inputViewModel.applydepartment['id'],
          leaveDuringAgent: this.inputViewModel.applyholidaymember,
          reason: this.inputViewModel.applychangenote,
          startTime: this.inputViewModel.applyholidaystarttime,
          endTime: this.inputViewModel.applyholidayendtime,
          leaveDays: this.inputViewModel.applyholidaydays,
          address: this.inputViewModel.applyholidayaddress,
          phone: this.inputViewModel.applyholidaytel,
          duty: this.inputViewModel.applyholidayjob,
          jobDate: this.inputViewModel.applyholidayjointime,
          jobYear: this.inputViewModel.applyholidayworkage,
          annualLeaveDays: this.inputViewModel.applyholidayyeardays,
          auditPersonId: this.inputViewModel.applyholidayyeardays,
        };
        //收文传阅列表
        this.http.postJson('oa', 'leave/insert.do', params).then(res => {
          if (res['success']) {
            this.dialog.showAlert("提示","数据提交成功").then(()=>{
              this.navCtrl.pop();
            });
          } else {
            this.dialog.showAlert("提示",res["msg"]).then(()=>{
            });
          }
        });
      });
    }
  }

  //检查输入
  checkInput() {

    if (this.inputViewModel.applyapplytypes === "") {
      this.dialog.showToast("top", "请选择请假种类"); return false;
    }
    if (this.inputViewModel.applycompany === "") {
      this.dialog.showToast("top", "请选择申请公司"); return false;
    }
    if (this.inputViewModel.applydepartment === "") {
      this.dialog.showToast("top", "请选择申请部门"); return false;
    }
    if (this.inputViewModel.applychangenote === "") {
      this.dialog.showToast("top", "请输入请假事由"); return false;
    }
    if (this.inputViewModel.applyholidaystarttime === "") {
      this.dialog.showToast("top", "请选择开始时间"); return false;
    }
    if (this.inputViewModel.applyholidayendtime === "") {
      this.dialog.showToast("top", "请选择结束时间"); return false;
    }
    if (this.inputViewModel.applyholidaydays === "") {
      this.dialog.showToast("top", "请输入请假天数"); return false;
    }
    if (this.inputViewModel.applyholidayaddress === "") {
      this.dialog.showToast("top", "请输入家庭住址"); return false;
    }
    if (this.inputViewModel.applyholidaytel === "") {
      this.dialog.showToast("top", "请输入联系电话"); return false;
    }
    if (this.inputViewModel.applyholidayjob === "") {
      this.dialog.showToast("top", "请输入职务"); return false;
    }
    if (this.inputViewModel.applyholidayjointime === "") {
      this.dialog.showToast("top", "请选择入职时间"); return false;
    }
    if (this.inputViewModel.applyholidayworkage === "") {
      this.dialog.showToast("top", "请输入工龄"); return false;
    }
    if (this.inputViewModel.applyholidayyeardays === "") {
      this.dialog.showToast("top", "请输入年休假标准"); return false;
    }
    if (this.inputViewModel.applynextman === "") {
      this.dialog.showToast("top", "请选择下一步审批人"); return false;
    }
    return true;
  }
}
